/**
 * zhanjun
 */

$(function() {
	
	var idUser=getUrlParamValue("idUser");
	if(idUser==""){
		alert("传入的参数不正确，请重新打开！");
		location.href='user-admin.html';
	}
	$("#idUser").attr("value",idUser);
	var userName=getUrlParamValue("userName");
	
	$('#sp_groupname').html(decodeURI(userName));
	
	/*
	 * 加载已经有的组
	 */
	$("#ObjSelect")[0].options.length = 0;
	$.getJSON("../user/ser_loadDetails.do?id="+idUser, function(data) {
		$.each(data.tbGroupSet, function(i) {
			$("<option value='" + data.tbGroupSet[i].idGroup + "'>"+ data.tbGroupSet[i].groupName + "</option>").appendTo("#ObjSelect");
		});
	});

	/*
	 * 加载系统下对应的组
	 */			
	$("#SrcSelect")[0].options.length = 0;
	$.getJSON("../group/ser_getGroupList.do", function(data) {			
		$.each(data.options, function(i) {
			$("<option value='" + data.options[i].key + "'>"+ data.options[i].value + "</option>").appendTo("#SrcSelect");
		});
	});
	
	var loadRoleSelect=function(idgroup){
		$("#roleList").html("");
		$.getJSON("../group/ser_getGroupRoles.do?idGroups="+idgroup, function(data) {
			$.each(data.options, function(i) {
				$("<tr><td>"+ data.options[i].value + "</tr></td>").appendTo("#roleList");
			});
		});
		
	};
	
	/*
	 * 绑定下拉事件
	 */
	
	$('#SrcSelect').bind('change', function() {
		if($("#SrcSelect").val()!=""){
			var idsys=$("#SrcSelect").val();
			loadRoleSelect(idsys);
		}
	});

	

	
	
	
	$("#btn_reset").click(function() {
		var dg =  frameElement.dg;
		dg.reload();		
	});
	/*
	 * 保存事件
	 */

	$("#btn_submit").click(function() {
		var postdata = "idUser="+idUser;
		var ObjSelect =document.getElementById("ObjSelect");
		var itemField=new Array();
		var itemName=new Array();
		if(ObjSelect&&ObjSelect.options&&ObjSelect.options.length>0){
			var len = ObjSelect.options.length;
			for(var j=0; j<len; j++){
				itemField.push(ObjSelect.options[j].value);
				itemName.push(ObjSelect.options[j].text);
			}
		}
		postdata += "&idGroup="+itemField.join(",");
			
			$.ajax({
				type : "POST",
				url : "../user/ser_userGroupSave.do",
				data : postdata,
				success : function(msg) {
					alert(msg);
					location.reload();					
				}
			});
		
	});

});
